﻿using System;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Framework.Core;
using Yoozoo.Gameplay.RTS;

namespace Gameplay.PVE.Survivor
{
    public class GuideLineManager: BattleSingleton<GuideLineManager>
    {
        private GameObject drawlineObject;
        private DrawLineTool drawLineTool;
        private bool drawLineToolInited = false;
        
        public override void Initialize()
        {
            InitDrawLineTool();
        }

        public void BindCamera(Camera camera)
        {
            drawLineTool.SetFollowTarget(camera.transform);
        }
        
        public void InitDrawLineTool()
        {
            // 画线工具

            //TODO:drawlineTool初始化要放到这里么？PVE也在用
            DrawLineTool drawLineTool = DrawLineTool.GetInstance();
            if (!drawLineTool.IsInitialized())
            {
                drawLineTool.Initialize();
            }

            drawLineTool.SetHeight(0.1f);
            if (CameraManager.Instance.MainCamera)
            {
                drawLineTool.SetFollowTarget(CameraManager.Instance.MainCamera.transform);
            }

            drawLineTool.materialType = 0;
            drawLineTool.LINE_WIDTH = 0.08f;

            drawlineObject = drawLineTool.gameObject;
            this.drawLineTool = drawLineTool;
            drawLineToolInited = true;
            CameraManager.Instance.SetDrawLineWidthAndHeight();
        }

        public int ShowLine(Vector3 start, Vector3 end)
        {
            var line = RTSClassPoolManager.GetLineInfo();
            line.color = Color.yellow;
            line.lineType = LineType.arrow;
            line.speed = 1;
            line.isPart = true;
            line.isHeadPart = true;
            line.team = 0;
            line.startPos = new Vector2(start.x,start.z);
            line.endPos = new Vector2(end.x,end.z);
            float scale = 1f;
            var lineId = (int) DrawLineTool.GetInstance().AddLine(line);
            RTSClassPoolManager.RecoverLineInfo(line);
            return lineId;
        }
        
        public int ShowLine(List<Vector3> path)
        {
            var line = RTSClassPoolManager.GetLineInfo();
            line.color = Color.yellow;
            line.lineType = LineType.arrow;
            line.speed = 1;
            line.isPart = true;
            line.isHeadPart = true;
            line.team = 0;
            float scale = 1f;
            var lineId = (int) DrawLineTool.GetInstance().AddMultiLines(line,0,path);
            RTSClassPoolManager.RecoverLineInfo(line);
            return lineId;
        }

        public void HideLine(int lineId)
        {
            DrawLineTool.GetInstance().DelMultiLines((uint)lineId,true);
        }

        public void HideSingleLine(int lineId)
        {
            DrawLineTool.GetInstance().DelLine((uint)lineId);
        }
        
        public int ShowLine(Vector3[] path)
        {
            var line = RTSClassPoolManager.GetLineInfo();
            line.color = Color.yellow;
            line.lineType = LineType.arrow;
            line.speed = 1;
            line.isPart = true;
            line.isHeadPart = true;
            line.team = 0;
            float scale = 1f;
            var lineId = (int) DrawLineTool.GetInstance().AddMultiLines(line,path);
            RTSClassPoolManager.RecoverLineInfo(line);
            return lineId;
        }
        
        public override void Dispose()
        {
            if (drawlineObject != null)
            {
                this.drawLineTool.Dispose();
                drawLineTool = null;
                drawlineObject = null;
            }
        }
    }
}
